# -*- coding: utf-8 -*-
#
# @Author: CPS
# @email: 373704015@qq.com
# @Date: 
# @Last Modified by: CPS
# @Last Modified time: 2021-08-13 22:12:57.599852
# @file_path "D:\CPS\IDE\sublime Text\sublime_text_4113.21_win64_patched\sublime_text\Data\Packages\testt_switch_language"
# @Filename "testt_switch_language.py"
# @Description: 功能描述
#



import sublime, sublime_plugin
import os, sys

if sys.platform != 'win32': 
    exit()

if int(sublime.version()) < 3176:
    raise ImportWarning("本插件不支持当前版本，请使用大于等于3176的sublime Text")

# 插件名称
PLUGIN_NAME = "testt_switch_language"

# 为了引入插件目录下的 pywin32模块 添加插件目录到环境变量
sys.path.append(os.path.join(sublime.packages_path(),PLUGIN_NAME))

# 引入模块
import Pywin32.setup
import win32api, win32gui
from win32con import WM_INPUTLANGCHANGEREQUEST

# 设置语言
LANG = { "zh": 0x0804, "en": 0x0409 }

def init():
    # 为了引入插件目录下的 pywin32模块 添加插件目录到环境变量
    sys.path.append(os.path.join(sublime.packages_path(),PLUGIN_NAME))
    
    # 引入模块
    import Pywin32.setup
    import win32api, win32gui
    from win32con import WM_INPUTLANGCHANGEREQUEST

def change_language(lang:str="en") -> bool:
    
    global LANG
    HWND = win32gui.GetForegroundWindow()
    LANGUAGE_CODE = LANG[lang.lower()] or LANG["en"]

    res = win32api.PostMessage(
        HWND,
        WM_INPUTLANGCHANGEREQUEST,
        0,
        LANGUAGE_CODE
    )
    return res


def check_system_language() -> str:
    """
    @Description 获取当前系统的语言


    returns `{str}` {description}

    """
    import ctypes

    dll_h = ctypes.windll.kernel32

    res = hex(dll_h.GetSystemDefaultUILanguage())
    return res


class TesttSwicthLanguageEventer(sublime_plugin.EventListener):
    def on_activated_async(self, view):
        """
        @Description 每当视图切换，都执行切换一次语言

        """
        change_language('en')

        
class TesttSwitchLanguageCommand(sublime_plugin.TextCommand):
    def run(self, view):
        change_language('en')

if ( __name__ == "__main__"):
    change_language('en')
